import java.util.*;


public class Leetcode567 {

    public static void main(String[] args) {
        System.out.println(checkInclusion("ccc", "cbac"));
    }

    public static boolean checkInclusion(String s1, String s2) {
        Map<Character, Integer> characterIntegerMap = new HashMap<>();
        char[] s1Chars = s1.toCharArray();
        for (char character : s1Chars) {
            characterIntegerMap.put(character, characterIntegerMap.getOrDefault(character, 0) + 1);
        }

        char[] s2Chars = s2.toCharArray();

        int n1 = s1.length();
        int n2 = s2.length();

        for (int i = 0; i <= n2 - n1; i++) {
            Map<Character, Integer> cp = new HashMap<>(characterIntegerMap);
            for (int j = i; j < i + n1; j++) {
                if (cp.containsKey(s2Chars[j])) {
                    int charNumber = cp.get(s2Chars[j]) - 1;
                    if (charNumber == 0) {
                        cp.remove(s2Chars[j]);
                    } else {
                        cp.put(s2Chars[j], charNumber);
                    }
                } else {
                    break;
                }
            }
            if (cp.size() == 0) {
                return true;
            }
        }

        return false;
    }
}
